package org.vacoor.xqq.ui.dynamictree;

import org.vacoor.xqq.core.bean.ClientType;
import org.vacoor.xqq.core.bean.Status;
import org.vacoor.xqq.ui.util.ImageUtil;

import javax.swing.*;
import java.awt.*;

/**
 * User: Vacoor
 */
public class Buddy extends IdentifiableAndObservableData {
    public static final Dimension SMALL_AVATAR_SIZE = new Dimension(20, 20);
    public static final Dimension BIG_AVATAR_SIZE = new Dimension(40, 40);
    public static final Icon DEFAULT_AVATAR = new ImageIcon(Buddy.class.getClassLoader().getResource("images/avatar_default_40_40.gif"));
    public static final Icon ICON_CLIENT_MOBILE = new ImageIcon(Buddy.class.getClassLoader().getResource("images/client_type/online_phone.png"));
    public static final Icon ICON_CLIENT_WEB = new ImageIcon(Buddy.class.getClassLoader().getResource("images/client_type/online_webqq.png"));
    public static final Icon ICON_CLIENT_PAD = new ImageIcon(Buddy.class.getClassLoader().getResource("images/client_type/online_pad.png"));

    private String nick;    // 昵称
    private String mark;    // 标注名称
    private String additional;  // 附加内容
    private Icon avatar;    // 原始头像

    private Icon smallAvatar;   // 小头像
    private Icon bigAvatar;     // 大头像

    private Status status = Status.OFFLINE;
    private ClientType clientType = ClientType.UNKNOW;

    public Buddy(long id) {
        super(id);
    }

    public Buddy(long id, String nick, String mark, String additional, Icon avatar) {
        super(id);
        setNick(nick);
        setMark(mark);
        setAdditional(additional);
        setAvatar(avatar != null ? avatar : DEFAULT_AVATAR);
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        String old = this.nick;
        this.nick = nick;
        firePropertyChange("nick", old, nick);
    }

    public String getMark() {
        return mark;
    }

    public void setMark(String mark) {
        String old = this.mark;
        this.mark = mark;
        firePropertyChange("mark", old, mark);
    }

    public String getAdditional() {
        return additional;
    }

    public void setAdditional(String additional) {
        String old = this.additional;
        this.additional = additional;
        firePropertyChange("additional", old, additional);
    }

    public Icon getAvatar() {
        return avatar;
    }

    public void setAvatar(Icon avatar) {
        Icon old = this.avatar;
        this.avatar = avatar;
        prepareAvatar();
        firePropertyChange("avatar", old, avatar);
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        Status old = this.status;
        this.status = status;
        prepareAvatar();
        firePropertyChange("status", old, status);
    }

    public ClientType getClientType() {
        return clientType;
    }

    public void setClientType(ClientType clientType) {
        ClientType old = this.clientType;
        this.clientType = clientType;
        prepareAvatar();
        firePropertyChange("clientType", old, clientType);
    }

    public Icon getSmallAvatar() {
        return smallAvatar;
    }

    public Icon getBigAvatar() {
        return bigAvatar;
    }

    private void prepareAvatar() {
        if (avatar == null) {
            smallAvatar = null;
            bigAvatar = null;
            return;
        }

        final Image avatarImg = ((ImageIcon) avatar).getImage();
        smallAvatar = new ImageIcon(ImageUtil.resize(avatarImg, SMALL_AVATAR_SIZE));
        bigAvatar = new ImageIcon(ImageUtil.resize(avatarImg, BIG_AVATAR_SIZE));

        if (Status.isOfflineStatus(status)) {
            return;
        }

        Image watermark = null;
        int x = avatarImg.getWidth(null) - ICON_CLIENT_MOBILE.getIconWidth() - 1;
        int y = avatarImg.getHeight(null) - ICON_CLIENT_MOBILE.getIconHeight() + 1;
        switch (clientType) {
            case MOBILE:
                watermark = ((ImageIcon) ICON_CLIENT_MOBILE).getImage();
                break;
            case WEBQQ:
                watermark = ((ImageIcon) ICON_CLIENT_WEB).getImage();
                x -= 2;
                y += 2;
                break;
            case PAD:
                watermark = ((ImageIcon) ICON_CLIENT_PAD).getImage();
                y += 6;
                break;
            default:
        }

        if (watermark == null) {
            return;
        }

        final Image watermarkImg = ImageUtil.drawWatermark(avatarImg, watermark, x, y, watermark.getWidth(null), watermark.getHeight(null), null);
        bigAvatar = new ImageIcon(ImageUtil.resize(watermarkImg, BIG_AVATAR_SIZE));
        smallAvatar = new ImageIcon(ImageUtil.resize(watermarkImg, SMALL_AVATAR_SIZE));
    }

    /*
    @Override
    public String toString() {
        return nick;
    }
    */
}
